package cn.jiemusic.controller;

import cn.hutool.core.util.StrUtil;
import cn.jiemusic.common.R;
import cn.jiemusic.domain.po.Music;
import cn.jiemusic.domain.vo.MusicVo;
import cn.jiemusic.service.MusicService;
import cn.jiemusic.utils.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/music")
@Slf4j
public class MusicController {
    @Autowired
    private MusicService musicService;

    @Autowired
    private AudioUtils audioUtils;


    @Value("${project-config.base-path}")
    private String basePath;

    @PostMapping("/upload")
    public R<String> uploadFile(
            HttpServletRequest req,
            @RequestParam("name") String name,  // 歌曲名
            @RequestParam("artist") String artist,  // 歌手
            @RequestParam("lyrics") String lyrics,  // 歌词
            @RequestParam("audio") MultipartFile audio  // 音频文件
    ) throws IOException {
        Music music = new Music();
        music.setName(name);
        music.setArtist(artist);
        music.setLyrics(lyrics);
        music.setUploader(JWTUtils.getUsernameByRequest(req));
        musicService.save(music);

        TransferUtils.upload(audio, basePath, music.getId(), "audio");

        Music temp = audioUtils.extractMusicInfo(audio, music.getId());
        if (temp != null) {
            temp.setId(music.getId());
            musicService.updateById(temp);
        }


        return R.success("上传成功");
    }

    @GetMapping("/list")
    public R<List<MusicVo>> getList(HttpServletRequest req) {
        String uid = JWTUtils.getIdByRequest(req);
        List<MusicVo> likeList = musicService.getLikeList(uid);

        return R.success(likeList);
    }

    @GetMapping("/random-list")
    public R<List<MusicVo>> randomList(HttpServletRequest req, int n) {

        String uid = JWTUtils.getIdByRequest(req);

        return R.success(musicService.getRandomList(uid, n));
    }

    @GetMapping
    public R<MusicVo> getMusicVo(HttpServletRequest req, String mid) {

        String uid = JWTUtils.getIdByRequest(req);
        MusicVo musicVo = musicService.getMvoById(mid, uid);
        return R.success(musicVo);
    }

    @GetMapping("/s-list")
    public R<List<MusicVo>> getSearchList(String name) {
        LambdaQueryWrapper<Music> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        if (StrUtil.isNotEmpty(name)) {
            queryWrapper.like(Music::getName, name)
                    .or().like(Music::getAlbum, name)
                    .or().like(Music::getArtist, name);
        }

        queryWrapper.orderByDesc(Music::getCreateTime);

        List<Music> musicList = musicService.list(queryWrapper);
        List<MusicVo> musicVoList = BeanCopyUtils.copyBeanList(musicList, MusicVo.class);


        return R.success(musicVoList);
    }

}
